#ifndef ACCOUNT_H
#define ACCOUNT_H

#include <QObject>
#include<QList>
#include<QPair>
#include<QDebug>
#include<QMap>
#include<QTimer>
#include"Thread/servermessageadapter.h"
#include "Association.h"
#include"Thread/UIManager.h"
#include<QMutex>
enum LoginStatus
{
    LOGIN,
    LOGOUT
};
class Account : public QObject
{
    Q_OBJECT
private:
    QString passwd;
    QString name;
    QString uid_me;
    LoginStatus status;
    QTimer *timer;

    QList<Association> list_association_group;
    QList<Association> list_association_friend;
    static Account* account;
    explicit Account();

    QList<Association> association_list;
    QMap<QString ,QString> apply_friend_map;//apply_sender,gid
public:
    static Account * getInstance()
    {
        static QMutex mutex_instance;
        if(account==nullptr)
        {
            mutex_instance.lock();
            if(account==nullptr)
            {
                account =new Account();
            }
            mutex_instance.unlock();
        }


        return account;
    }
    void getName(QString& _name)
    {
        _name = name;
    }
    void getPasswd(QString& _passwd)
    {
        _passwd = passwd;
    }
    void getMyUid(QString& _UID)
    {
        _UID = uid_me;
    }
    void getStatus(LoginStatus& _status)
    {
        _status = status;
    }
    void getFriend(QMap<QString,QString>& friend_map )
    {
//        qDebug()<<"get friend success";
        friend_map.clear();
        for(int i=0;i<list_association_friend.size();i++)
        {
//            qDebug()<<"friend size"<<list_association_friend.size();
            for(int j=0;j<list_association_friend.at(i).uid_member_of_this.size();j++)
            {
                if(list_association_friend.at(i).uid_member_of_this.at(j).uid!=this->uid_me)
                {
                    friend_map.insert(list_association_friend.at(i).uid_member_of_this.at(j).uid,list_association_friend.at(i).uid_member_of_this.at(j).name);
                }

            }


        }
        qDebug()<<"get friend success";
    }
    void getGroup(QMap<QString,QString>& group_map )
    {
        group_map.clear();
        for(int i=0;i<list_association_group.size();i++)
        {
//            qDebug()<<"friend size"<<list_association_friend.size();
                group_map.insert(list_association_group.at(i).uid_group,list_association_group.at(i).name);

        }
        qDebug()<<"get group success";
    }
    void getGid(const QString uid,QString & gid)
    {
        QString temp ="";
        for(int i=0;i<list_association_friend.size();i++)
        {
            for(int j=0;j<list_association_friend.at(i).uid_member_of_this.size();j++)
            {
                if(uid ==list_association_friend.at(i).uid_member_of_this.at(j).uid )
                    temp =list_association_friend.at(i).uid_group;
            }
        }
        gid = temp;
    }
    void login(QString name,QString uid_me,QString passwd);
    void setGroup(Association ass);
    void setFriend(Association ass);

    void insertPost(QString uid,QString gid)
    {
        apply_friend_map.insert(gid,uid);
        qDebug()<<"post map insert <"<<gid<<","<<uid<<">";
    }
    void getPost(QMap<QString ,QString>& post_map)
    {
        post_map = apply_friend_map;
        apply_friend_map.clear();
    }
    void getA_Gid_FriendName(QString gid,QString& name)
    {
        QString temp = "UNKNOWN_NAME";
        for(int i=0;i<list_association_friend.size();i++)
        {
            if(list_association_friend.at(i).uid_group == gid)
                temp = list_association_friend.at(i).name;

        }
        name = temp;
    }
    void getA_Uid_GroupFriendName(QString uid,QString & name)
    {
        QString temp = "UNKNOWN_NAME";

        static QMutex mutex;

        mutex.lock();
        for(int i=0;i<list_association_group.size();i++)
        {

            for(int j=0;j<list_association_group.at(i).uid_member_of_this.size();j++)
            {

                if(list_association_group.at(i).uid_member_of_this.at(j).uid==uid)
                    temp =list_association_group.at(i).uid_member_of_this.at(j).name;
            }
        }
        mutex.unlock();
        name = temp;
    }


signals:

};

#endif // ACCOUNT_H
